package com.jjiang.bbmall.seckill.controller;

import com.jjiang.bbmall.seckill.service.SeckillService;
import com.jjiang.bbmall.seckill.to.SeckillSkuRedisTo;
import com.jjiang.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {
    @Autowired
    SeckillService seckillService;

    /**
     * 返回当前时间可以参与的秒杀活动
     *
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/getCurrentSecKillSkus")
    public R getCurrentSecKillSkus() {
        List<SeckillSkuRedisTo> seckillSkuRedisTos = seckillService.getCurrentSecKillSkus();
        return R.ok().setData(seckillSkuRedisTos);
    }

    /**
     * 根据skuID查询该商品是否有秒杀活动
     *
     * @param skuId
     * @return
     */
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo secKillSkuRedisTo = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(secKillSkuRedisTo);
    }

    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId, @RequestParam("key") String key, @RequestParam("num") Integer num, Model model) {
        // 1.判断是否登录[引入了拦截器]
        // 2.秒杀
        String orderSn = seckillService.kill(killId, key, num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
